package com.venutech.gongan.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.venutech.gongan.dao.TbOpWorkOrderMapper;
import com.venutech.gongan.dao.TbWorkMapper;
import com.venutech.gongan.entity.TbOpWorkOrder;
import com.venutech.gongan.entity.TbWork;
import com.venutech.gongan.entity.Work;
import com.venutech.gongan.service.ITbOpWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 运维Service业务层处理
 * 
 * @author linxb
 * @date 2023-09-19
 */
@Service
public class TbOpWorkOrderServiceImpl implements ITbOpWorkOrderService
{
    @Autowired
    private TbOpWorkOrderMapper tbOpWorkOrderMapper;
    @Autowired
    private TbWorkMapper workMapper;

    /**
     * 查询运维
     * 
     * @param opWorkId 运维主键
     * @return 运维
     */
    @Override
    public TbOpWorkOrder selectTbOpWorkOrderByOpWorkId(Long opWorkId)
    {
        return tbOpWorkOrderMapper.selectTbOpWorkOrderByOpWorkId(opWorkId);
    }

    /**
     * 查询运维列表
     * 
     * @param TbOpWorkOrder 运维
     * @return 运维
     */
    @Override
    public List<TbOpWorkOrder> selectTbOpWorkOrderList(TbOpWorkOrder tbOpWorkOrder)
    {
        return tbOpWorkOrderMapper.selectTbOpWorkOrderList(tbOpWorkOrder);
    }

    /**
     * 新增运维
     * 
     * @param TbOpWorkOrder 运维
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbOpWorkOrder(TbOpWorkOrder tbOpWorkOrder)
    {
        tbOpWorkOrderMapper.insertTbOpWorkOrder(tbOpWorkOrder);
//        TbOpWorkOrder opWorkOrder=tbOpWorkOrderMapper.selectTbOpWorkOrderLast(tbOpWorkOrder);
        TbWork work=new TbWork();
        work.setWkSqUserId(tbOpWorkOrder.getRepairMan());
        work.setWkSqTime(tbOpWorkOrder.getRepairTime());
        work.setWkSpUserId(tbOpWorkOrder.getResponseMan());
        work.setWkContent(tbOpWorkOrder.getFaultRemark());
        work.setWkState((long)0);
        work.setOpWorkId(tbOpWorkOrder.getOpWorkId());
        work.setWkOrderType(0);//运维工单
        return workMapper.insertTbWork(work);
    }

    /**
     * 修改运维
     * 
     * @param TbOpWorkOrder 运维
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTbOpWorkOrder(TbOpWorkOrder tbOpWorkOrder)
    {
        tbOpWorkOrderMapper.updateTbOpWorkOrder(tbOpWorkOrder);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        TbWork tbWork=workMapper.selectWorkByTbOpWorkOrder(tbOpWorkOrder);
        TbWork work=new TbWork();
        work.setWkId(tbWork.getWkId());
        work.setWkSqUserId(tbOpWorkOrder.getRepairMan());
        work.setWkSqTime(tbOpWorkOrder.getRepairTime());
        work.setWkSpUserId(tbOpWorkOrder.getResponseMan());
        work.setWkContent(tbOpWorkOrder.getFaultRemark());
        work.setWkState((long)0);
        work.setOpWorkId(tbOpWorkOrder.getOpWorkId());
        work.setWkOrderType(0);//运维工单
        return workMapper.updateTbWork(work);
    }

    /**
     * 批量删除运维
     * 
     * @param opWorkIds 需要删除的运维主键
     * @return 结果
     */
    @Override
    public int deleteTbOpWorkOrderByOpWorkIds(String opWorkIds)
    {
        return tbOpWorkOrderMapper.deleteTbOpWorkOrderByOpWorkIds(Convert.toStrArray(opWorkIds));
    }

    /**
     * 删除运维信息
     * 
     * @param opWorkId 运维主键
     * @return 结果
     */
    @Override
    public int deleteTbOpWorkOrderByOpWorkId(Long opWorkId)
    {
        return tbOpWorkOrderMapper.deleteTbOpWorkOrderByOpWorkId(opWorkId);
    }

    @Override
    @Transactional
    public int approveTbOpWorkOrder(TbOpWorkOrder tbOpWorkOrder) {
        tbOpWorkOrderMapper.updateTbOpWorkOrder(tbOpWorkOrder);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        TbOpWorkOrder opWorkOrder=new TbOpWorkOrder();
        opWorkOrder.setOpWorkId(tbOpWorkOrder.getOpWorkId());
        TbWork tbWork=workMapper.selectWorkByTbOpWorkOrder(opWorkOrder);
        TbWork work=new TbWork();
        work.setWkState((long)tbOpWorkOrder.getWk_state());
        work.setWkSpTime(date);
        work.setWkId(tbWork.getWkId());
        return workMapper.updateTbWork(work);
    }
}
